package vip.xiaonuo.smzq.modular.xxgs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.smzq.modular.kcl.entity.Ks;
import vip.xiaonuo.smzq.modular.kcl.mapper.KsMapper;
import vip.xiaonuo.smzq.modular.xxgs.entity.Lsks;
import vip.xiaonuo.smzq.modular.xxgs.entity.param.LsksPageQuery;
import vip.xiaonuo.smzq.modular.xxgs.entity.Xxgssj;
import vip.xiaonuo.smzq.modular.xxgs.entity.vo.KsxxgslxVo;
import vip.xiaonuo.smzq.modular.xxgs.mapper.LsksMapper;
import vip.xiaonuo.smzq.modular.xxgs.mapper.XxgssjMapper;
import vip.xiaonuo.smzq.modular.xxgs.service.ILsksService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 信息公示--绿色矿山 服务实现类
 *
 * @author sqq
 * @since 2024-07-19
 */
@Service
public class LsksServiceImpl extends ServiceImpl<LsksMapper, Lsks> implements ILsksService {
    @Resource
    private XxgssjMapper xxgssjMapper;
    @Resource
    private LsksMapper lsksMapper;
    @Resource
    private KsMapper ksMapper;

    @Override
    public Page<Lsks> page(LsksPageQuery lsksPageQuery) {
        Page<Lsks> page = new Page<>(lsksPageQuery.getCurrent(), lsksPageQuery.getSize());
        LambdaQueryWrapper<Lsks> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(lsksPageQuery.getNd() != null, Lsks::getFbsj, String.valueOf(lsksPageQuery.getNd()))
                .eq(Lsks::getDelflag, 0)
                .like(lsksPageQuery.getKeyword() != null && !lsksPageQuery.getKeyword().isEmpty(), Lsks::getKsmc, lsksPageQuery.getKeyword());
        if (lsksPageQuery.getXzqh() != null) {
            queryWrapper .in(Lsks::getXzqdm, Arrays.asList(lsksPageQuery.getXzqh().split(",")));
        }
        queryWrapper.orderByDesc(Lsks::getFbsj);

        return this.page(page, queryWrapper);
    }

    @Override
    public void updateDelete(String pkid) {
        Lsks lsks = new Lsks();
        lsks.setDelflag(1);

        LambdaQueryWrapper<Lsks> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Lsks::getPkid, pkid);
        this.update(lsks, queryWrapper);
    }

    @Override
    public Page<Lsks> page(Lsks lsks) {
        return null;
    }

    @Override
    public KsxxgslxVo getKsxxgslxVo() {
        KsxxgslxVo ksxxgslxVo = new KsxxgslxVo();
        ksxxgslxVo.setSflrsx(false);
        ksxxgslxVo.setSflryc(false);
        ksxxgslxVo.setSflks(false);
        // 获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        if (saBaseLoginUser.getXkzh() != null) {
            String xkzh = saBaseLoginUser.getXkzh();
            return getKsxxgslx(xkzh);
        }

        return ksxxgslxVo;
    }

    /**
     * 矿业权人右上角信息公示类型
     * @param xkzh 许可证号
     */
    @Override
    public KsxxgslxVo getKsxxgslx(String xkzh) {

        KsxxgslxVo ksxxgslxVo = new KsxxgslxVo();
        ksxxgslxVo.setSflks(false);
        ksxxgslxVo.setSflrsx(false);
        ksxxgslxVo.setSflryc(false);
        try{
            //根据许可证号查询对应矿山
            LambdaQueryWrapper<Ks> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Ks::getXkzh, xkzh);
            queryWrapper.orderByDesc(Ks::getNd);
            queryWrapper.last("limit 1");
            Ks ks = ksMapper.selectOne(queryWrapper);

            //绿色矿山
            LambdaQueryWrapper<Lsks> queryWrapperLsks = new LambdaQueryWrapper<>();
            if(ks.getKsmc()!=null&&!ks.getKsmc().equals("")){
                queryWrapperLsks.eq(Lsks::getKsmc, ks.getKsmc());
                queryWrapperLsks.orderByDesc(Lsks::getFbsj);
                queryWrapperLsks.eq(Lsks::getDelflag, 0);
                queryWrapperLsks.last("limit 1");
                Lsks lsks = lsksMapper.selectOne(queryWrapperLsks);
                if (lsks != null){
                    ksxxgslxVo.setSflks(true);
                }

            }

           //是否列入失信
            LambdaQueryWrapper<Xxgssj> queryWrapperXxgssj = new LambdaQueryWrapper<>();
            queryWrapperXxgssj.eq(Xxgssj::getKyqrmc, ks.getKyqr());
            queryWrapperXxgssj.eq(Xxgssj::getDelflag, 0);
            queryWrapperXxgssj.eq(Xxgssj::getSflrsxmd,1);
            queryWrapperXxgssj.eq(Xxgssj::getZh,ks.getShxydm());
            queryWrapperXxgssj.orderByDesc(Xxgssj::getLrrq);
            queryWrapperXxgssj.last("limit 1");
            Xxgssj xxgssj = xxgssjMapper.selectOne(queryWrapperXxgssj);
            if (xxgssj != null){
                ksxxgslxVo.setSflrsx(true);
            }

            //是否列入异常
            LambdaQueryWrapper<Xxgssj> queryWrapperYc = new LambdaQueryWrapper<>();
            queryWrapperYc.eq(Xxgssj::getKyqrmc, ks.getKyqr());
            queryWrapperYc.eq(Xxgssj::getDelflag, 0);
            queryWrapperYc.eq(Xxgssj::getSflrycml,1);
            queryWrapperYc.eq(Xxgssj::getZh,ks.getShxydm());
            queryWrapperYc.orderByDesc(Xxgssj::getLrrq);
            queryWrapperYc.last("limit 1");
            Xxgssj yc = xxgssjMapper.selectOne(queryWrapperYc);
            if (yc != null){
                ksxxgslxVo.setSflryc(true);
            }

        }catch (Exception e){
          e.printStackTrace();
        }


       return ksxxgslxVo;
    }










}
